
//////////Need to modify for each application////////////////
var APP_ID = 33558;
var SERVER_NAME = "http://harnivorouspartner.appspot.com/";
/////////////////////////////////////////////////////////////

var nickname = "";
var username="";
var GAME_TIME = 4;
var ANSWER_NUM = 4;
var gameTime;
var TOP_POST = "game";
var GAME_POST = "game";
var RESULT_POST = "game";
var PRIVACY_POST="privacy";
var RESULT_IMG_POST = "html/result";
var GAME_IMG_POST = "html/game";
var PC_POST="760.PNG";
var SMARTPHONE_POST="320.JPG";
var AGENT_POST=PC_POST;
var userAgent="";

function staticPageRequest(pageID){
	var REQUEST_URL = SERVER_NAME + pageID;
	var params = {};
	params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.GET;
   params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.TEXT;
 
	params[gadgets.io.RequestParameters.AUTHORIZATION] = gadgets.io.AuthorizationType.SIGNED;
	gadgets.io.makeRequest(REQUEST_URL, function(response){
		/// no action
		if (response.data) {
		
			document.getElementById("mainContents").innerHTML = response.data;
			document.getElementById("mainContents").style.backgroundImage = 'url("")';
			
		}
		else {
			var err = response.errors[0];
			window.alert("タイムアウトしました。しばらく時間をおいてからリロードしてください。");
		}
	}, params);
}
/*
function privacyRequest(){
	var REQUEST_URL = SERVER_NAME + PRIVACY_POST;
	var params = {};
	params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.GET;
   params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.TEXT;
 
	params[gadgets.io.RequestParameters.AUTHORIZATION] = gadgets.io.AuthorizationType.SIGNED;
	gadgets.io.makeRequest(REQUEST_URL, function(response){
		/// no action
		if (response.data) {
		
			document.getElementById("mainContents").innerHTML = response.data;
			document.getElementById("mainContents").style.backgroundImage = 'url("")';
			
		}
		else {
			var err = response.errors[0];
			window.alert("タイムアウトしました。しばらく時間をおいてからリロードしてください。");
		}
	}, params);
}
*/
function createTopPage(){

    var req = opensocial.newDataRequest();
    var params = {};
    params[opensocial.DataRequest.PeopleRequestFields.PROFILE_DETAILS] = [opensocial.Person.Field.HAS_APP];
    req.add(req.newFetchPersonRequest(opensocial.IdSpec.PersonId.VIEWER, params), "viewer");
    req.send(function(data){
        if (!data.hadError()) {
            var item = data.get("viewer");
            if (item.hadError()) {
                var code = item.getErrorCode();
                // error logic                
            }
            else {
            
                var viewer = item.getData();
                hasApp = viewer.getField(opensocial.Person.Field.HAS_APP);
                // if viewer doesn't have application, move to application registration page
                if (hasApp == "false") {
                    location.href = "http://mixi.jp/view_appli.pl?id=" + APP_ID;
                }
                else {
                    makeTopPage();
                }
            }
        }
    });
}


function makeTopPage(){

    var url = SERVER_NAME + TOP_POST;
    var params = {};
    params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.GET;
    params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.TEXT;
    params[gadgets.io.RequestParameters.AUTHORIZATION] = gadgets.io.AuthorizationType.SIGNED;
    
    gadgets.io.makeRequest(url, function(response){
        if (response.data) {
        
            document.getElementById("mainContents").innerHTML = response.data;
            gameTime = 0;
            //  getUserInfo();
        
        }
        else {
            var err = response.errors[0];
            window.alert("タイムアウトしました。しばらく時間をおいてからリロードしてください。");
        }
    }, params);
}

/*
function privacyRequest(url){
	var REQUEST_URL = SERVER_NAME + PRIVACY_POST;
	var params = {};
	params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.GET;
	params[gadgets.io.RequestParameters.AUTHORIZATION] = gadgets.io.AuthorizationType.SIGNED;
	gadgets.io.makeRequest(url, function(response){
		/// no action
		if (response.data) {
		
			document.getElementById("mainContents").innerHTML = response.data;
			document.getElementById("mainContents").style.backgroundImage = 'url("")';
			
		}
		else {
			var err = response.errors[0];
			window.alert("タイムアウトしました。しばらく時間をおいてからリロードしてください。");
		}
	}, params);
}
*/

function createGamePage(){
    var REQUEST_URL = SERVER_NAME + GAME_POST;
    var params = {};
    params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.POST;
    params[gadgets.io.RequestParameters.AUTHORIZATION] = gadgets.io.AuthorizationType.SIGNED;
    var gender;
    //detect gender
    if (gameTime == 0) {
        if (document.getElementsByName("gender")[0].checked) {
            gender = "0";
        }
        else {
            gender = "1";
        }
    }
    else {
        gender = document.getElementById("gender").value;
    }
    
    
    var post_data = {
        questionNum: document.getElementById("questionNum").value,
        sessionID: document.getElementById("sessionID").value,
        nickname: document.getElementById("nickname").value,
		username: document.getElementById("username").value,
        gender: gender
    };
	if(gameTime>0){
		var answer=checkAnswer();
		post_data["answer"]=answer;
	}
    params[gadgets.io.RequestParameters.POST_DATA] = gadgets.io.encodeValues(post_data);
  
    gadgets.io.makeRequest(REQUEST_URL, function(response){
        /// no action
        if (response.data) {
        
            document.getElementById("mainContents").innerHTML = response.data;
            gameTime = gameTime + 1;

            if (gameTime > GAME_TIME) {
                document.getElementById("mainContents").style.backgroundImage = 'url("' + SERVER_NAME + RESULT_IMG_POST + AGENT_POST+'")';
            }
            else {
                document.getElementById("mainContents").style.backgroundImage = 'url("' + SERVER_NAME + GAME_IMG_POST + AGENT_POST+'")';
                
            }
        }
        else {
            var err = response.errors[0];
            window.alert("タイムアウトしました。しばらく時間をおいてからリロードしてください。");
        }
    }, params);
}

function checkAnswer(){
    for (i = 0; i < ANSWER_NUM; i++) {
        if (document.getElementsByName("answer")[i].checked) {
            return String(i);
        }
    }
}

function getUserInfo(){
    var req = opensocial.newDataRequest();
    req.add(req.newFetchPersonRequest(opensocial.IdSpec.PersonId.VIEWER), "viewer");
    req.send(function(data){
        if (!data.hadError()) {
            var item = data.get("viewer");
            if (item.hadError()) {
                var code = item.getErrorCode();
                // error logic                
            }
            else {
                var viewer = data.get("viewer").getData();
                username = viewer.getDisplayName();
           //     document.getElementById("nicknameArea").innerHTML = nickname + "さん";
           //     document.getElementById("nickname").value = nickname;
                
            }
        }
    });
}




/**
 *  refer to http://developer.mixi.co.jp/appli/spec/pc/invite_appli
 */
function inviteFriends(){
    //var params = {};
    //params[opensocial.IdSpec.Field.USER_ID] = inviteFriendIDs;
    //var recipients = opensocial.newIdSpec(params);
    //opensocial.requestShareApp(recipients, null, function(response){
    opensocial.requestShareApp("VIEWER_FRIENDS", null, function(response){
        if (response.hadError()) {
            var errCode = response.getErrorCode();
        }
        else {
            var recipientIds = response.getData()["recipientIds"];
        }
    });
}

function moveToTop(topURL){
	window.parent.location.href=topURL;
}

/**
 * refer to http://developer.mixi.co.jp/appli/spec/pc/send_voice
 * @param {Object} index
 */
function doVoice(voiceText){
    var params = {};
    // When you want to add URL in voice, please 
    params[mixi.Status.Field.URL] = "http://mixi.jp/view_appli.pl?id=" + APP_ID + "&appParams=";
    params[mixi.Status.Field.MOBILE_URL] = "http://ma.mixi.net/" + APP_ID + "/?url=";
    params[mixi.Status.Field.TOUCH_URL] = "http://mixi.jp/view_appli.pl?id=" + APP_ID + "&appParams=";
    mixi.requestUpdateStatus(voiceText, function(response){
        if (response.hadError()) {
            var code = response.getErrorCode();
            var msg = response.getErrorMessage();
        }
        else {
        
        }
    }, params);
}


